import smtplib
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart

smtpserver = 'smtp.126.com'  # 发送邮箱服务器

user = 'lichunmei55@126.com'  # 发送邮箱用户名
password = ' '  # 发送邮箱密码
sender = 'lichunmei55@126.com'  # 发送邮箱
receiver = '787360118@qq.com'  # 接收邮箱
subject = 'python email from lichunmei with file'  # 发送邮件主题

# 得到要发送的附件
sendfile = open('C:\\wksPy\\user_info.txt', 'rb').read()
att = MIMEText(sendfile, 'base64', 'utf-8')
att["Content-Type"] = 'application/octet-stream'
att["Content-Disposition"] = 'attachment;filename="userinfo.txt"'
msgRoot = MIMEMultipart('related')
msgRoot['Subject'] = subject
msgRoot['from'] = sender
msgRoot['to'] = receiver
msgRoot.attach(att)

# 连接发送邮件
smtp = smtplib.SMTP()
smtp.connect(smtpserver)
smtp.login(user, password)
smtp.sendmail(sender, receiver, msgRoot.as_string())
smtp.quit()
